package com.yj.web.controllers.admin;

import com.yj.web.annotation.LoginRequired;
import com.yj.web.dao.po.admin.Agreement;
import com.yj.web.model.BaseWebResult;
import com.yj.web.service.AgreementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanlinyang on 2017/7/25.
 */
@Controller
@RequestMapping("/admin")
public class AgreementController {
    private static final Log logger = LogFactory.getLog(AgreementController.class);

    @Autowired
    private AgreementService agreementService;

    @LoginRequired
    @RequestMapping(value="/listAgreement", method = RequestMethod.POST)
    public @ResponseBody
    BaseWebResult listAgreement(@RequestParam("type") Integer type,
                               @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        BaseWebResult result = new BaseWebResult();
        List<Agreement> agreements = new ArrayList<>();
        try {
            agreements = agreementService.listAgreement(type, page, size);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        result.setData(agreements);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/countAgreement", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult countAgreement(@RequestParam("type") Integer type) {
        BaseWebResult result = new BaseWebResult();
        Integer num = agreementService.countAgreement(type);
        result.setData(num);
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/deleteAgreementById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult deleteAgreementById(@RequestParam("id") int id) {
        BaseWebResult result = new BaseWebResult();
        int b = agreementService.deleteAgreementById(id);
        if (0 == b) {
            result.setCd(1);
            result.setMsg("不能删除处于启用状态的协议");
            return result;
        }
        result.setData(b);
        return result;
    }

    @LoginRequired
    @RequestMapping(value="/insertAgreement", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult insertAgreement(@ModelAttribute("agreement") Agreement agreement) {
        BaseWebResult result = new BaseWebResult();

        try {
            long flag = agreementService.insertAgreement(agreement);
            int count = agreementService.countAgreement(agreement.getType());
            if (0 == flag && 0 == count) {
                result.setCd(1);
                result.setMsg("第一个协议必须处于启用状态");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("必须有且仅有一个协议处于启用状态");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateAgreement", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateAgreement(@ModelAttribute("agreement") Agreement agreement) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = agreementService.updateAgreement(agreement);
            if (true == flag) {
                result.setMsg("更新成功");
            } else {
                result.setCd(1);
                result.setMsg("必须有且仅有一个协议处于启用状态。");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/getAgreementById", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult getAgreementById(@RequestParam("id") int id) {
        BaseWebResult result = new BaseWebResult();
        try {
            Agreement agreement = agreementService.getAgreementById(id);
            result.setData(agreement);
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }

    @LoginRequired
    @RequestMapping(value = "/updateAgreementStatus", method = RequestMethod.POST)
    public @ResponseBody BaseWebResult updateAgreementStatus(@RequestParam("id") Integer id) {
        BaseWebResult result = new BaseWebResult();
        try {
            boolean flag = agreementService.updateAgreementStatus(id);
            if (false == flag) {
                result.setCd(1);
                result.setMsg("必须有且仅有一个协议处于启用状态。");
            } else {
                result.setMsg("Agreement状态更新成功");
            }
        } catch (Exception e) {
            result.setCd(1);
            result.setMsg("系统错误");
        }
        return result;
    }
}
